package com.gopay.checkorder.dao.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.checkorder.dao.BankAuthOrderDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.bank.BankOrderStatus;
import com.gopay.common.constants.bank.BankOrderType;
import com.gopay.common.domain.bank.BankAuthOrder;
import com.gopay.common.util.DateUtils;

/**
 * @BankAuthOrderDAOImpl.java
 * @author lilili
 * @2017年1月16日 上午10:30:09 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("bankAuthOrderDAO")
public class BankAuthOrderDAOImpl extends BaseDAOImpl<BankAuthOrder, String> implements BankAuthOrderDAO {

	@Override
	public int getCheckOrderCount(String orderStatus) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<BankAuthOrder> getCheckOrder(String orderStatus, int count, int pageIndex, int pageSize) {

		StringBuffer hql = new StringBuffer("");
		hql.append("from BankAuthOrder ");
		hql.append("where orderStatus = :orderStatus and totalCheckTimes = :totalCheckTimes ");
		hql.append(" and orderType = :orderType ");
		hql.append(buildBeginEndTimeClause());
		hql.append("order by orderId");

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderStatus", orderStatus);
		params.put("totalCheckTimes", count);
		params.put("orderType", BankOrderType.GOPAY_BIS_ORDER);

		List<BankAuthOrder> list = this.findByHql(hql.toString(), params, pageIndex, pageSize);

		return list;

	}

	private Date getNextDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		date = calendar.getTime();
		return date;
	}

	@Override
	public int getCheckOrderCount(Date date) {
		date = getNextDay(date);
		StringBuffer hql = new StringBuffer("");
		hql.append("select count(*) from BankAuthOrder ");
		hql.append("where orderStatus = :orderStatus ");
		hql.append(" and sendBankTm >= :date and totalCheckTimes >= :totalCheckTimes ");
		hql.append(" and totalCheckTimes <12 ");

		Map<String, Object> params = new HashMap<String, Object>();
		String status = BankOrderStatus.PROCESS;
		params.put("orderStatus", status);
		params.put("totalCheckTimes", 7);
		params.put("date", date);
		int rsCount = this.findCountByHql(hql.toString(), params);
		return rsCount;
	}

	@Override
	public List<BankAuthOrder> getCheckOrder(Date date, int pageIndex, int pageSize) {
		date = getNextDay(date);
		StringBuffer hql = new StringBuffer("");
		hql.append("from BankAuthOrder ");
		hql.append("where orderStatus =:orderStatus ");
		hql.append(" and sendBankTm >= :date ");
		hql.append(" and totalCheckTimes >= :totalCheckTimes ");
		hql.append(" and totalCheckTimes <12 ");
		hql.append("order by totalCheckTimes asc");

		Map<String, Object> params = new HashMap<String, Object>();
		String status = BankOrderStatus.PROCESS;
		params.put("orderStatus", status);
		params.put("date", date);
		params.put("totalCheckTimes", 7);
		List<BankAuthOrder> list = this.findByHql(hql.toString(), params, pageIndex, pageSize);

		return list;
	}

	private String buildBeginEndTimeClause() {
		Calendar calendar = Calendar.getInstance();

		// 开始时间--最近一天
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		String beginTime = DateUtils.format(calendar.getTime(), "yyyyMMddHHmmss");

		// 截至时间--90秒前
		calendar.setTime(new Date());
		calendar.add(Calendar.SECOND, -90);
		String endTime = DateUtils.format(calendar.getTime(), "yyyyMMddHHmmss");

		StringBuilder sb = new StringBuilder(" and sendBankTm <to_date('" + endTime + "','yyyyMMddHH24miss')");
		sb.append(" and sendBankTm >to_date('" + beginTime + "','yyyyMMddHH24miss')");

		return sb.toString();
	}
	
	    @Override
	    public int updateTotalCheckTimes(BankAuthOrder bankOrder) {
	    	StringBuffer hql = new StringBuffer();
	    	hql.append("update BankAuthOrder set ");
	    	hql.append("totalCheckTimes = :totalCheckTimes ,");
	    	hql.append("lastUpdTs = :lastUpdTs ");
	    	hql.append("where outOrderId = :outOrderId");
	    	
	    	Map<String, Object> params = new HashMap<String, Object>();
	    	params.put("totalCheckTimes", bankOrder.getTotalCheckTimes() + 1);
	    	params.put("lastUpdTs", DateUtils.getSystemDate());
	    	params.put("outOrderId", bankOrder.getOutOrderId());
	    	
	    	int result = this.updateByHql(hql.toString(),params);
	    	
	    	return result;
	    }
	    
	    @Override
	    public int updateBackStatus(BankAuthOrder authOrder) {
	        
	        Date curDate = DateUtils.getSystemDate();

	        StringBuffer hql = new StringBuffer();
	        hql.append("update BankAuthOrder set orderStatus = :orderStatus ,");
	        hql.append("lastUpdTs = :lastUpdTs ");
	        hql.append("where orderId = :orderId ");
	        
	        Map<String, Object> params = new HashMap<String, Object>();
	        params.put("orderStatus", authOrder.getOrderStatus());
	        params.put("lastUpdTs", curDate);
	        params.put("orderId", authOrder.getOrderId());

	        return this.updateByHql(hql.toString(), params);
	    }

}
